package net.jxtz.jxtzos.authentication;


import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.mapper.redis.RedisMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * 退出登陆处理器
 *
 * @author a123
 */
@Slf4j
public class MyLogoutHandler implements LogoutHandler {

    /**
     * redis
     */
    private final RedisMapper redisMapper;

    public MyLogoutHandler(RedisMapper redisMapper){
        this.redisMapper = redisMapper;
    }

    @Override
    public void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) {
        log.info("进入退出拦截器");
        String jwtToken = httpServletRequest.getHeader(JwtUtils.TOKEN_HEAD).replaceFirst(JwtUtils.TOKEN_PREFIX,"");
        String userName = JwtUtils.subject(jwtToken);
        log.info("清空用户：<" + userName + "> 的jwt_token和csrf_token");
        redisMapper.deleteKey(userName + JwtUtils.TOKEN_PARAM_PREFIX);
        redisMapper.deleteKey(userName + MyCsrfTokenRepository.CSRF_PARAM);
    }
}
